package com.enqualcomm.kids.mvp.pay;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.enqualcomm.kids.R;

/**
 * Created by Yinzhiqun on 2016/8/8.
 */
public class PayPopupWindow extends PopupWindow implements View.OnClickListener {
    private TextView price_tv;
    private ImageView wxpay_iv;
    private ImageView alipay_iv;
    private Listener listener;
    public PayPopupWindow(View contentView) {
        super(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    }

    public static PayPopupWindow create(Activity activity){
        View contentView = View.inflate(activity, R.layout.pop_pay_type,null);
        PayPopupWindow popupWindow = new PayPopupWindow(contentView);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.price_tv =  (TextView) contentView.findViewById(R.id.price_tv);
        popupWindow.wxpay_iv = (ImageView) contentView.findViewById(R.id.wxpay_iv);
        popupWindow.alipay_iv = (ImageView) contentView.findViewById(R.id.alipay_iv);
        contentView.findViewById(R.id.ok_btn).setOnClickListener(popupWindow);
        contentView.findViewById(R.id.wxpay_rl).setOnClickListener(popupWindow);
        contentView.findViewById(R.id.alipay_rl).setOnClickListener(popupWindow);

        return popupWindow;
    }

    public void setPrice(String price){
        price_tv.setText(price);
    }

    public void show(Activity activity){
        showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM,0,0);
    }

    public void setListener(Listener listener){
        this.listener = listener;
    }

    private boolean isAlipay;

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ok_btn:
                dismiss();
                if (listener!=null){
                    if (isAlipay) {
                        listener.onAlipay();
                    }else {
                        listener.onWxpay();
                    }
                }
                break;
            case R.id.wxpay_rl:
                isAlipay = false;
                alipay_iv.setImageResource(R.drawable.pay_normal);
                wxpay_iv.setImageResource(R.drawable.pay_checked);
                break;
            case R.id.alipay_rl:
                isAlipay = true;
                wxpay_iv.setImageResource(R.drawable.pay_normal);
                alipay_iv.setImageResource(R.drawable.pay_checked);
                break;
        }
    }

    public interface Listener{

        void onAlipay();

        void onWxpay();
    }
}
